﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Hrm.Core;
using AutoMapper.QueryableExtensions;

namespace Hrm.Services
{
    public class ClassMateService :BaseService<ClassMate>, IClassMateService
    {
        IClassMateHrmDbRepository _classMateHrmDbRepository;
        IClassMateExtentionHrmDbRepository _classMateExtentionHrmDbRepository;
        ISchoolHrmDbRepository _schoolHrmDbRepository;

        public ClassMateService(IClassMateHrmDbRepository classMateHrmDbRepository, IClassMateExtentionHrmDbRepository classMateExtentionHrmDbRepository, ISchoolHrmDbRepository schoolHrmDbRepository)
            : base(classMateHrmDbRepository)
        {
            _classMateHrmDbRepository = classMateHrmDbRepository;
            _classMateExtentionHrmDbRepository = classMateExtentionHrmDbRepository;
            _schoolHrmDbRepository = schoolHrmDbRepository;
        }

        public PageResponse<ClassMateDto> GetClassMatePage(PageRequest<ClassMateRequest> request)
        {
            var query = this.GetAllAbledAndOrderByCreatetime().OrderBy(p=>p.Name).AsQueryable();
            if (!string.IsNullOrEmpty(request.filter.Name))
            {
                query = query.Where(p => p.Name.Contains(request.filter.Name));
            }
            if (request.filter.SchoolId > 0)
            {
                query = query.Where(p => p.Schools.Any(q => q.Id == request.filter.SchoolId));
            }
            if (!string.IsNullOrEmpty(request.filter.Phone))
            {
                query = query.Where(p => p.Phone == request.filter.Phone);
            }
            var data = query.Project().To<ClassMateDto>();
            PageResponse<ClassMateDto> result = new PageResponse<ClassMateDto>(data, request.draw, request.start, request.length);
            return result;
        }
    }
}
